package deibay.server.io;

import java.util.LinkedList;
import java.util.List;
import java.util.Vector;

import org.jdom.Document;
import org.jdom.Element;

import deibay.datamodel.Category;
import deibay.datamodel.Item;

public class XmlDataBackup {

	private Document doc = null;
	private Element backup = null;
	private Element categories = null;
	private Element products = null;
	
	public XmlDataBackup()
	{
		
	}	
	
	public void addProduct(Item p)
	{
		XmlProduct xmlProd = new XmlProduct(p);
		products.addContent(xmlProd.getXmlProduct());
		XmlIO.writeXmltoFile(doc, XmlIO.XML_DATA_BACKUP);
	}
	
	public void addCategory(Category c)
	{
		XmlCategory xmlCat = new XmlCategory(c);
		categories.addContent(xmlCat.getXmlCategory());
		XmlIO.writeXmltoFile(doc, XmlIO.XML_DATA_BACKUP);
	}
	
	public void createXmlBackup()
	{
		doc = new Document();
		backup = new Element("Backup");
		doc.setRootElement(backup);	
		categories = new Element("Categories");
		products = new Element("Products");
		backup.addContent(categories);
		backup.addContent(products);
	}
	
	public void loadXmlRegister()
	{
		doc = XmlIO.readXML(XmlIO.XML_DATA_BACKUP);		
		if(doc == null)
		{
			createXmlBackup();
			XmlIO.writeXmltoFile(doc, XmlIO.XML_DATA_BACKUP);
			doc = XmlIO.readXML(XmlIO.XML_DATA_BACKUP);
		}
		backup = doc.getRootElement();
		categories = backup.getChild("Categories");
		products = backup.getChild("Products");
	}
	
	public Vector<Item> getItems()
	{
		Vector<Item> items = new Vector<Item>();
		List<Element> elements = products.getChildren();
		for(Element e: elements)
		{
			XmlProduct product = new XmlProduct(e);
			items.add(product.xmlToObject());
		}
		return items;
	}

	public void updateItem(String id, int value, String bidder) {		
		List<Element> elements = products.getChildren();
		for(Element e: elements)
		{
			Element eId = e.getChild("id");
			if(eId.getValue().equals(id))			
			{
				Element eValue = e.getChild("actualprice");
				eValue.removeContent();
				eValue.addContent(""+value);				
				
				Element eBidder = e.getChild("login_bidder");
				eBidder.removeContent();
				eBidder.addContent(""+bidder);
				
				XmlIO.writeXmltoFile(doc, XmlIO.XML_DATA_BACKUP);
			}
		}
	}

	public void updateSubscribedUsersToItem(String user, String id) {
		List<Element> elements = products.getChildren();
		for(Element e: elements)
		{
			Element eId = e.getChild("id");
			if(eId.getValue().equals(id))			
			{
				Element eValue = e.getChild("subscribed_users");
				List<Element> users = eValue.getChildren();
				for(Element el: users)
				{
					if(el.getValue().equals(user))
						return;
				}
				Element eUser = new Element("user");
				eUser.addContent(user);
				eValue.addContent(eUser);
				XmlIO.writeXmltoFile(doc, XmlIO.XML_DATA_BACKUP);
			}
		}		
	}

	public void removeProduct(Item p) {		
		List<Element> elements = (List<Element>) new LinkedList<Element>(products.getChildren()).clone();
		Element elementToRemove = null;
		
		for(Element element: elements)
		{
			Element eId = element.getChild("id");
			if(eId.getValue().equals(p.getId()))
			{
				products.removeContent(element);
			}
		}

		XmlIO.writeXmltoFile(doc, XmlIO.XML_DATA_BACKUP);
	}
	
	
}
